import Vue from 'vue'
import VueRouter from 'vue-router'
import main from '../views/MainView.vue'
import home from '../views/home/HomeView.vue'
import mall from '../views/mall/MallView.vue'
import user from '../views/user/UserView.vue'
import page1 from '../views/other/PageOne'
import page2 from '../views/other/PageTwo'
import login from '../views/login/LoginView'


Vue.use(VueRouter)

const routes = [
  {
    path: '*',
    component: () => import('../views/404.vue')
  },
  { path: '/', redirect: '/login' },
  { 
    path: '/login', 
    name: login, 
    component: () => import('../views/login/LoginView.vue')
  },
  {
    path: '/main',
    name: main,
    component: () => import('../views/MainView.vue'),
    children:[
      {
        path: '/home',
        name: home,
        component: () => import('../views/home/HomeView.vue')
      },
      {
        path: '/mall',
        name: mall,
        component: () => import('../views/mall/MallView.vue')
      },
      {
        path: '/user',
        name: user,
        component: () => import('../views/user/UserView.vue')
      },
      {
        path: '/page1',
        name: page1,
        component: () => import('../views/other/PageOne.vue')
      },
      {
        path: '/page2',
        name: page2,
        component: () => import('../views/other/PageTwo.vue')
      }
    ]
  }
  // ,
  // {
  //   path: '/newView',
  //   name: 'newView',
  //   component: () => import('../views/newView/newViewOne.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, form, next) => {
  if(to.path === '/login') return next()

  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')

  next()
})

export default router
